package com.dd.census.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dd.census.entity.SysMenu;
import com.dd.census.service.SysMenuService;
import com.dd.census.tools.TokenHelper;
import com.dd.census.tools.resopnse.ResultResponse;
import com.dd.census.tools.resopnse.emums.ResultStatus;

@RestController
@RequestMapping(value = "census/sysMenu")
public class SysMenuController {

	private static final Logger logger = LoggerFactory.getLogger(SysMenuController.class);

	@Autowired
	private SysMenuService sysMenuService;

	@RequestMapping("selectPage")
	public ResultResponse<Object> selectPage(@RequestBody SysMenu sysMenu, HttpServletRequest request) {
		try {
			return ResultResponse.buildSuccess(sysMenuService.selectPage(sysMenu));

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("selectName")
	public ResultResponse<Object> selectName(@RequestBody SysMenu sysMenu, HttpServletRequest request) {
		try {
			return ResultResponse.buildSuccess(sysMenuService.selectName(sysMenu));

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("selectChild")
	public ResultResponse<Object> selectChild(@RequestBody SysMenu sysMenu, HttpServletRequest request) {
		try {
			return ResultResponse.buildSuccess(sysMenuService.selectChild(sysMenu));

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("insert")
	public ResultResponse<Object> insert(HttpServletRequest request, @RequestBody SysMenu sysMenu) {

		try {
			// 插入数据
			sysMenu.setCreateBy(TokenHelper.getUserId(request));

			return ResultResponse.buildSuccess(sysMenuService.insert(sysMenu));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("/{id}")
	public ResultResponse<Object> getById(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {

			return ResultResponse.buildSuccess(sysMenuService.selectById(id));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("update")
	public ResultResponse<Object> update(HttpServletRequest request, @RequestBody SysMenu sysMenu) {

		try {
			// 修改数据
			sysMenu.setUpdateBy(TokenHelper.getUserId(request));
			return ResultResponse.buildSuccess(sysMenuService.update(sysMenu));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}
	
	@RequestMapping("delete")
	public ResultResponse<Object> delete(HttpServletRequest request, @RequestBody SysMenu sysMenu) {

		try {
			// 修改数据
			sysMenu.setUpdateBy(TokenHelper.getUserId(request));

			return ResultResponse.buildSuccess(sysMenuService.delete(sysMenu));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("selectAll")
	public ResultResponse<Object> selectAll(HttpServletRequest request, @RequestBody SysMenu sysMenu) {
		try {

			return ResultResponse.buildSuccess(sysMenuService.selectAll(TokenHelper.getUserId(request)));

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}
	
	@RequestMapping("selectList")
	public ResultResponse<Object> selectList(@RequestBody SysMenu sysMenu, HttpServletRequest request) {
		try {
			return ResultResponse.buildSuccess(sysMenuService.selectList(sysMenu));

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

}
